var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var paymentInfoAgent = require('./requireServers/paymentInfoAgent.js');
var CodeAgent = require('./requireServers/verficationCodeAgent.js');
var payAgent = require('./requireServers/payAgent.js');
var installorderAgent = require('./requireServers/installorderAgent.js');

var util = require('util');

// 创建 application/x-www-form-urlencoded 编码解析
var urlencodedParser = bodyParser.urlencoded({
  extended: false
});

app.use(express.static('public'));
app.use(bodyParser.json());
app.get('/dist/test/fixedLayout.html', function(req, res) {
  res.sendFile(__dirname + "/dist/test/fixedLayout.html");
});
app.get('/dist/test/flowLayout.html', function(req, res) {
  res.sendFile(__dirname + "/dist/test/flowLayout.html");
});
app.get('/dist/test/learnVue.html', function(req, res) {
  res.sendFile(__dirname + "/dist/test/learnVue.html");
});

app.get('/dist/payinstallment/test1.html', function(req, res) {
  res.sendFile(__dirname + "/dist/payinstallment/test1.html");
});
app.get('/dist/payinstallment/payIndex.html', function(req, res) {
  res.sendFile(__dirname + "/dist/payinstallment/payIndex.html");
});


app.get('/test/testwebSocket.html', function(req, res) {
  res.sendFile(__dirname + "/test/testwebSocket.html");
});
/*app.post('/dist/payinstallment/test1.html', function(req, res) {
  res.sendFile(__dirname + "/dist/payinstallment/test1.html");
});*/
app.get('/dist/payinstallment/test2.html', function(req, res) {
  res.sendFile(__dirname + "/dist/payinstallment/test2.html");
});
app.get('/dist/payinstallment/test3.html', function(req, res) {
  res.sendFile(__dirname + "/dist/payinstallment/test3.html");
});
app.get('/dist/payinstallment/test4.html', function(req, res) {
  res.sendFile(__dirname + "/dist/payinstallment/test4.html");
});
app.get('/dist/payinstallment/indextestVue.html', function(req, res) {
  res.sendFile(__dirname + "/dist/payinstallment/indextestVue.html");
});
app.get('/dist/payinstallment/payPrompt.html', function(req, res) {
  res.sendFile(__dirname + "/dist/payinstallment/payPrompt.html");
});
app.post('/dist/payinstallment/payPrompt.html', function(req, res) {
  res.sendFile(__dirname + "/dist/payinstallment/payPrompt.html");
});

app.get('/dist/test/loading2.html', function(req, res) {
  res.sendFile(__dirname + "/dist/test/loading2.html");
});
app.get('/dist/test/testCss.html', function(req, res) {
  res.sendFile(__dirname + "/dist/test/testCss.html");
});


app.get('/dist/css/test/fixedLayout.css', function(req, res) {
  res.sendFile(__dirname + "/dist/css/test/fixedLayout.css");
});
app.get('/dist/css/faceDetect/faceMain.css', function(req, res) {
  res.sendFile(__dirname + "/dist/css/faceDetect/faceMain.css");
});



app.get('/dist/css/payinstallment/test1.css', function(req, res) {
  res.sendFile(__dirname + "/dist/css/payinstallment/test1.css");
});
app.get('/dist/css/payinstallment/payIndex.css', function(req, res) {
  res.sendFile(__dirname + "/dist/css/payinstallment/payIndex.css");
});

app.get('/dist/css/payinstallment/ie8.css', function(req, res) {
  res.sendFile(__dirname + "/dist/css/payinstallment/ie8.css");
});

app.get('/dist/css/payinstallment/PIE.htc', function(req, res) {
  res.sendFile(__dirname + "/dist/css/payinstallment/PIE.htc");
});
app.get('/dist/css/payinstallment/test2.css', function(req, res) {
  res.sendFile(__dirname + "/dist/css/payinstallment/test2.css");
});
app.get('/dist/css/payinstallment/test3.css', function(req, res) {
  res.sendFile(__dirname + "/dist/css/payinstallment/test3.css");
});
app.get('/dist/css/vendors/bootstrap.css', function(req, res) {
  res.sendFile(__dirname + "/dist/css/vendors/bootstrap.css");
});

app.get('/dist/js/libs/jquery-1.12.0.min.js', function(req, res) {
  res.sendFile(__dirname + "/dist/js/libs/jquery-1.12.0.min.js");
});
app.get('/dist/js/libs/jquery.qrcode.min.js', function(req, res) {
  res.sendFile(__dirname + "/dist/js/libs/jquery.qrcode.min.js");
});

app.get('/dist/js/libs/bootstrap-3.3.0.js', function(req, res) {
  res.sendFile(__dirname + "/dist/js/libs/bootstrap-3.3.0.js");
});
app.get('/dist/js/libs/Validform_v5.3.2_min.js', function(req, res) {
  res.sendFile(__dirname + "/dist/js/libs/Validform_v5.3.2_min.js");
});

app.get('/dist/js/payInstallment/urlVal.js', function(req, res) {
  res.sendFile(__dirname + "/dist/js/payInstallment/urlVal.js");
});
app.get('/dist/js/payInstallment/common.js', function(req, res) {
  res.sendFile(__dirname + "/dist/js/payInstallment/common.js");
});
app.get('/dist/js/payInstallment/common.js.min.js', function(req, res) {
  res.sendFile(__dirname + "/dist/js/payInstallment/common.js.min.js");
});



app.get('/dist/js/payInstallment/PIE.js', function(req, res) {
  res.sendFile(__dirname + "/dist/js/payInstallment/PIE.js");
});
app.get('/dist/js/payInstallment/main.min.js', function(req, res) {
  res.sendFile(__dirname + "/dist/js/payInstallment/main.min.js");
});
app.get('/dist/js/payInstallment/mainVue.min.js', function(req, res) {
  res.sendFile(__dirname + "/dist/js/payInstallment/mainVue.min.js");
});
app.get('/dist/js/payInstallment/prompt.min.js', function(req, res) {
  res.sendFile(__dirname + "/dist/js/payInstallment/prompt.min.js");
});

app.get('/dist/js/learnVue/common.js', function(req, res) {
  res.sendFile(__dirname + "/dist/js/learnVue/common.js");
});
app.get('/dist/js/learnVue/testVue.min.js', function(req, res) {
  res.sendFile(__dirname + "/dist/js/learnVue/testVue.min.js");
});



app.get('/dist/images/payinstallment/tonglianshangwu_logo.png', function(req, res) {
  res.sendFile(__dirname + "/dist/images/payinstallment/tonglianshangwu_logo.png");
});
app.get('/dist/images/payinstallment/tonglianzhifu_logo.png', function(req, res) {
  res.sendFile(__dirname + "/dist/images/payinstallment/tonglianzhifu_logo.png");
});
app.get('/dist/images/payinstallment/grzfwsxt.png', function(req, res) {
  res.sendFile(__dirname + "/dist/images/payinstallment/grzfwsxt.png");
});
app.get('/dist/images/payinstallment/tishi_icon.png', function(req, res) {
  res.sendFile(__dirname + "/dist/images/payinstallment/tishi_icon.png");
});
app.get('/dist/images/payinstallment/tishi_icon2.png', function(req, res) {
  res.sendFile(__dirname + "/dist/images/payinstallment/tishi_icon2.png");
});

app.get('/dist/images/payinstallment/logo.gif', function(req, res) {
  res.sendFile(__dirname + "/dist/images/payinstallment/logo.gif");
});
app.get('/dist/images/payinstallment/loding.gif', function(req, res) {
  res.sendFile(__dirname + "/dist/images/payinstallment/loding.gif");
});

app.get('/dist/images/payinstallment/logo.png', function(req, res) {
  res.sendFile(__dirname + "/dist/images/payinstallment/logo.png");
});
app.get('/dist/images/payinstallment/qietu_beijing_icon.png', function(req, res) {
  res.sendFile(__dirname + "/dist/images/payinstallment/qietu_beijing_icon.png");
});
app.get('/dist/images/payinstallment/wancheng_icon.png', function(req, res) {
  res.sendFile(__dirname + "/dist/images/payinstallment/wancheng_icon.png");
});
app.get('/dist/images/pageLoading1.jpg', function(req, res) {
  res.sendFile(__dirname + "/dist/images/pageLoading1.jpg");
});
app.get('/dist/images/pageLoading2.jpg', function(req, res) {
  res.sendFile(__dirname + "/dist/images/pageLoading2.jpg");
});
app.get('/dist/images/payinstallment/wechat_QR.jpg', function(req, res) {
  res.sendFile(__dirname + "/dist/images/payinstallment/wechat_QR.jpg");
});
app.get('/dist/images/payinstallment/shibai_icon.png', function(req, res) {
  res.sendFile(__dirname + "/dist/images/payinstallment/shibai_icon.png");
});


app.get('/dist/images/error.png', function(req, res) {
  res.sendFile(__dirname + "/dist/images/error.png");
});
app.get('/dist/images/right.png', function(req, res) {
  res.sendFile(__dirname + "/dist/images/right.png");
});
app.get('/dist/images/test/test2.jpg', function(req, res) {
  res.sendFile(__dirname + "/dist/images/test/test2.jpg");
});
app.get('/dist/images/test/test1.jpg', function(req, res) {
  res.sendFile(__dirname + "/dist/images/test/test1.jpg");
});
app.get('/dist/images/blue@2x.png', function(req, res) {
  res.sendFile(__dirname + "/dist/images/blue@2x.png");
});


app.post('/payinstallment/payInstallmentInfoServlet', urlencodedParser, function(req, res) {
  res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});//设置response编码为utf-8
  console.log("-----req-----");
  console.log(util.inspect(req.body));
  // 输出 JSON 格式
 /* var response = {
    "code": "0000",
    "msg": "",
    "data": {
      "orderId": "201702061602372528",
      "merName": "æµ‹è¯•æ±‡åˆ†æœŸ",
      "amount": "8000",
      "nper": "6",
      "unalter": "creditNo,creditName",
      "tradeDate": "2017-02-06 16:02:37",
      "returnUrl": "http://localhost:8080/allinpay-online/",
      "custName": "å¯å¯",
      "credCardno": "6259986239282080",
      "idno": "340603199402064797",
      "hpno": "17073547336",
      "cvv": "",
      "vaPerd": "",
      "simplPC": "0",
      "rateList": [{
        "nper": "3",
        "custRate": "12.26",
        "amtLowLimit": "600",
        "amtUplimit": "15000",
        "poundage": "326.94",
        "eachamount": "2993.60",
        "totalamount": "8980.80"
      }, {
        "nper": "6",
        "custRate": "0",
        "amtLowLimit": "1000",
        "amtUplimit": "20000",
        "poundage": "0.00",
        "eachamount": "1333.33",
        "totalamount": "8000.00"
      }, {
        "nper": "9",
        "custRate": "12",
        "amtLowLimit": "600",
        "amtUplimit": "20000",
        "poundage": "106.67",
        "eachamount": "995.55",
        "totalamount": "8960.00"
      }, {
        "nper": "10",
        "custRate": "9",
        "amtLowLimit": "600",
        "amtUplimit": "20000",
        "poundage": "72.00",
        "eachamount": "872.00",
        "totalamount": "8720.00"
      }, {
        "nper": "12",
        "custRate": "0",
        "amtLowLimit": "600",
        "amtUplimit": "25000",
        "poundage": "0.00",
        "eachamount": "666.66",
        "totalamount": "8000.00"
      }, {
        "nper": "18",
        "custRate": "0",
        "amtLowLimit": "600",
        "amtUplimit": "20000",
        "poundage": "0.00",
        "eachamount": "444.44",
        "totalamount": "8000.00"
      }, {
        "nper": "24",
        "custRate": "20",
        "amtLowLimit": "600",
        "amtUplimit": "20000",
        "poundage": "66.67",
        "eachamount": "400.00",
        "totalamount": "9600.00"
      }]
    }
  };
*/
  var theAgent = paymentInfoAgent.getAgent({}, req.body);
  theAgent.on("databack", function(data) {
    console.log("datback......");
    console.log('data: ' + data);
    res.end(data);
  });
  theAgent.request();
  //  console.log(JSON.stringify(response));
//    res.end(JSON.stringify(response));

});



app.post('/payinstallment/verificationCodeServlet', urlencodedParser, function(req, res) {
  res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});//设置response编码为utf-8
  console.log("取验证码条件" + util.inspect(req.body));
  var agentObjet = CodeAgent.getAgent({},req.body);
  agentObjet.on("databack", function(data) {
    console.log("datback......");
    console.log('data: ' + data);
    res.end(data);
  });
  agentObjet.request();
});

app.post('/payinstallment/payInstallmentServlet', urlencodedParser, function(req, res) {
  res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});//设置response编码为utf-8
  console.log("-----req-----");
  console.log(util.inspect(req.body));
  // 输出 JSON 格式  payAgent
  var payAgentObject = payAgent.getAgent({}, req.body);
  payAgentObject.on("databack", function(data) {
    console.log("datback......");
    console.log('data: ' + data);
    res.end(data);
  });
  payAgentObject.request();

  /*  模拟数据
  var response = {
    error_business_response: {
      code: "0000",
      smg: "支付成功",
    }
  };
  console.log(response);
  res.end(JSON.stringify(response));
  */
});

app.post('/assetbm/payinstallment/OrderInstallPay/pcinstallorder.do', urlencodedParser, function(req, res) {
  res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});//设置response编码为utf-8
  console.log("-----req-----");
  console.log(util.inspect(req.body));
  // 输出 JSON 格式  payAgent
  var installorderAgentObject = installorderAgent.getAgent({}, req.body);
  installorderAgentObject.on("databack", function(data) {
    console.log("datback......");
    console.log('data: ' + data);
    res.end(data);
  });
  installorderAgentObject.request();

  /*  模拟数据
  var response = {
    error_business_response: {
      code: "0000",
      smg: "支付成功",
    }
  };
  console.log(response);
  res.end(JSON.stringify(response));
  */
});


app.post('/liveDec/GetCheckNumServlet', urlencodedParser, function(req, res) {
  res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});//设置response编码为utf-8
  // 输出 JSON 格式
  response = {
    validate_data: "5876"
  };
  console.log(response);
  res.end(JSON.stringify(response));
});

app.post('/liveDec/youTuDetectServlet', urlencodedParser, function(req, res) {
  res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});//设置response编码为utf-8
  returnData = {
    code: "01"
  };
  res.end(JSON.stringify(returnData));
});


var server = app.listen(9013, function() {

  var host = server.address().address;
  var port = server.address().port;

  console.log("应用实例，访问地址为 http://%s:%s", host, port);

});